package com.lingyun.financialmanagement.service.pojo;

import com.baomidou.mybatisplus.generator.IFill;
import com.lingyun.financialmanagement.dao.mapper.BillMapper;
import com.lingyun.financialmanagement.dao.mapper.UsersMapper;
import com.lingyun.financialmanagement.dao.pojo.Bill;
import com.lingyun.financialmanagement.dao.pojo.OperationLog;
import com.lingyun.financialmanagement.dao.pojo.Users;
import com.lingyun.financialmanagement.service.BillService;
import com.lingyun.financialmanagement.utils.TokenGetUser;
import com.lingyun.financialmanagement.vo.ErrorCode;
import com.lingyun.financialmanagement.vo.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.info.BuildProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${redis.token.tokenAccountPrefix}")
    private String tokenAccountPrefix;

    @Autowired
    private UsersMapper usersMapper;


    @Autowired
    private OperationLogServiceImpl operationLogServiceImpl;

    @Override
    public Result selectAllBill() {
        List<Bill> bills = billMapper.selectList(null);
        return Result.success(bills);
    }

    @Override
    @Transactional
    public Result addBill(Bill bill) {

        String name = bill.getName();
        // 名称检测
        if (name == null || name.isEmpty()) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }

        Bill bill1 = billMapper.selectByName(bill.getName());
        // 检查名字是否重复
        if (bill1 != null) {
            return Result.fail(ErrorCode.NAME_UNIQUE.getCode(), ErrorCode.NAME_UNIQUE.getMsg());
        }

        Result usersResult = TokenGetUser.getUsers(request, redisTemplate, tokenAccountPrefix, usersMapper);
        if (!usersResult.isSuccess()) {
            return usersResult;
        }
        Users users = (Users) usersResult.getData();
        bill.setUsers(users.getId());

        int insert = 0;
        try{
            insert = billMapper.insert(bill);
        } catch (Exception e) {
            return Result.fail(ErrorCode.INSERT_ERROR.getCode(), ErrorCode.INSERT_ERROR.getMsg());
        }
        if (insert <= 0) {
            return Result.fail(ErrorCode.INSERT_ERROR.getCode(), ErrorCode.INSERT_ERROR.getMsg());
        }
        // 记录日志
        Result result = operationLogServiceImpl.insertLog(request, new OperationLog(null, null, null, null, null, "新增了发票" + bill.getName(), null));
        if (result.getCode() != 200){
            throw new RuntimeException(result.getMsg());
        }
        return Result.success(bill);
    }

    @Override
    public Result selectBillById(Integer id) {
        Bill bill = billMapper.selectById(id);
        return Result.success(bill);
    }
}
